package com.qinghuan.threads.chapterOne;

/**
 * @Author: dubao
 * @Date: 2022/5/17 20:06
 * @Description:
 *
 */

/**
 * 如果在sleep的过程中，发现条件已满足，就需要直接终止sleep，结束线程，
 * 这里就用到interrupt来处理
 */
public class Test1_7_2 {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("before sleep too long...");
                    Thread.sleep(300000);
                    System.out.println("after sleeping...");
                } catch (InterruptedException e) {
                    System.out.println("任务完成！");
                    return;
                }
                System.out.println("normal out!");
            }
        });

        //启动线程
        thread.start();
        //确保子线程进入休眠状态
        Thread.sleep(1000);
        //打断子线程的休眠，让子线程从sleep中返回
        thread.interrupt();
        //等待子线程执行完毕
        thread.join();
        System.out.println("main thread is over");
    }

}
